package com.test;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: denny
 * Date: 7/23/13
 * Time: 4:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class ValsSpreadGraph extends JPanel{
  int minVal;
  int maxVal;
  int markInterval;
  int retainVals;
  
  List<Double> vals = new ArrayList<>();
  
  public ValsSpreadGraph(int minVal, int maxVal, int markInterval, int retainVals) {
    setPreferredSize(new Dimension(500,40));
    setOpaque(true);
    setBackground(Color.WHITE);

    this.minVal=minVal;
    this.maxVal=maxVal;
    this.markInterval=markInterval;
    this.retainVals=retainVals;
  }
  
  void addVal(double val){
    vals.add(val);
    while(vals.size()>retainVals){
      vals.remove(0);
    }
    invalidate();
    repaint();
  }
  
  int valToX(double val){
    int total = getWidth() - 30;
    return (int)(5 + (val - minVal)*total/(maxVal-minVal));
  }

  public void paintComponent(Graphics g){
    g.setColor(Color.WHITE);
    g.fillRect(0,0,getWidth(), getHeight());
    g.setColor(Color.RED);
    for( int i=minVal; i<=maxVal; i+=markInterval ){
      g.drawLine(valToX(i),0,valToX(i),20);
      g.drawString("" + i, valToX(i), g.getFontMetrics().getHeight());
    }
    g.setColor(Color.BLUE);
    for( double v : vals ){
      //int rnd = 25 + (int)(Math.random()*10);
      g.drawLine(valToX(v), 25, valToX(v), 35);
    }
  }
}
